<?php
/**
 * @title 2192. 有向无环图中一个节点的所有祖先
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer $n
     * @param Integer[][] $edges
     * @return Integer[][]
     */
    function getAncestors($n, $edges) {
        /**
         * @since 2024-04-25 统计每个点的入度数量
         */
        $points = array_fill(0, $n, 0);
        $lines = [];
        foreach ($edges as list($s, $e)){
            $points[$e]++;
            $lines[$s][] = $e;
        }

        $ancestorArray = array_fill(0, $n, []);
        while(true){
            /**
             * @since 2024-04-25 入度为0的点，起点
             */
            $nodes = [];
            foreach ($points as $p=>$n){
                if($n==0) {
                    $nodes[] = $p;
                    unset($points[$p]);
                } else {}
            }
            if(empty($nodes)) break;

            /**
             * @since 2024-04-25 遍历找子节点
             */
            foreach ($nodes as $node){
                $aArray = $ancestorArray[$node];

                if(isset($lines[$node])){
                    foreach ($lines[$node] as $nextNode){
                        $points[$nextNode]--;
                        $ancestorArray[$nextNode] = array_unique(array_merge($ancestorArray[$nextNode], [$node], $aArray));
                    }
                } else {}
            }
        }

        /**
         * @since 2024-04-25 这些祖先节点 升序 排序
         */
        foreach ($ancestorArray as $k=>$aArray){
            sort($aArray);
            $ancestorArray[$k] = $aArray;
        }

        /**
         * @return
         */
        return $ancestorArray;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202404/2024.04.04.php
 */
$datas = [
    [8, [[0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->getAncestors(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
